package com.tao.mvpbaselibrary.app;

import android.app.Application;
import android.content.Context;

import androidx.multidex.MultiDex;

import com.tao.logger.log.Logger;
import com.tao.mvpbaselibrary.app.crash.CrashHandler;
import com.tao.mvpbaselibrary.basic.manager.lifecycle.LifecycleHandler;
import com.tao.mvpbaselibrary.basic.network.NetworkManager;
import com.tao.mvpbaselibrary.basic.utils.AppUtils;
import com.tao.mvpbaselibrary.basic.utils.ToastUtil;
import com.tao.mvpbaselibrary.lib_http.retrofit.AbstractDefaultNetProvider;
import com.tao.mvpbaselibrary.lib_http.retrofit.NetMgr;
import com.tao.mvpbaselibrary.lib_http.retrofit.RequestHandler;

import io.reactivex.functions.Consumer;
import io.reactivex.plugins.RxJavaPlugins;
import okhttp3.Interceptor;

public abstract class BasicApplication extends Application {
    private String tag = getClass().getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    private void init() {
        ToastUtil.init(this);
        AppUtils.init(this);
        initRxjavaCatch();
        initLogger();
        CrashHandler.getExceptionHandler().init(this);
        NetworkManager.getInstance().init(this);
        initNet();
        initDownloadManager();
        initLifecycle();
    }

    private void initRxjavaCatch() {
        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Logger.e("Rxjava gloable catch ", throwable);
                onRxError(throwable);
            }
        });
    }

    public void onRxError(Throwable throwable) {


    }

    private void initLogger() {
        Logger.init(this);
        Logger.setEnable(true);
        Logger.setPriority(Logger.MIN_LOG_PRIORITY);
    }


    private void initLifecycle() {
        registerActivityLifecycleCallbacks(LifecycleHandler.create().setApplicationRunCallback(new LifecycleHandler.Callback() {
            @Override
            public void inForeground() {
                Logger.i("inForeground");
            }

            @Override
            public void inBackgound() {
                Logger.i("inBackgound");
            }
        }));
    }

    public void initNet() {
        NetMgr.getInstance().registerProvider(new AbstractDefaultNetProvider() {
            @Override
            public RequestHandler configHandler() {
                return BasicApplication.this.configDefaultHandler();
            }

            @Override
            public String configBaseUrl() {
                return BasicApplication.this.configDefaultBaseUrl();
            }

            @Override
            public Interceptor[] configInterceptors() {
                return BasicApplication.this. configInterceptors();
            }
        });
    }

    public Interceptor[] configInterceptors() {
        return null;
    }

    protected abstract String configDefaultBaseUrl();

    protected abstract RequestHandler configDefaultHandler();

    /**
     * 初始化下载配置
     */
    private void initDownloadManager() {
//        Aria.init(this);
    }


}
